{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="float-right">
        <button type="button" id="button-upload" data-toggle="tooltip" title="{{ button_upload }}" class="btn btn-primary"><i class="fas fa-upload"></i></button>
      </div>
      <h1>{{ heading_title }}</h1>
      <ol class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
          <li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ol>
    </div>
  </div>
  <div class="container-fluid">
    {% if error_warning %}
      <div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> {{ error_warning }}
        <button type="button" class="close" data-dismiss="alert">&times;</button>
      </div>
    {% endif %}
    {% if success %}
      <div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> {{ success }}
        <button type="button" class="close" data-dismiss="alert">&times;</button>
      </div>
    {% endif %}
    <div class="card">
      <div class="card-header"><i class="fas fa-exchange-alt"></i> {{ heading_title }}</div>
      <div class="card-body">
        <form>
          <fieldset>
            <legend>{{ text_progress }}</legend>
            <div class="form-group row">
              <label class="col-sm-2 col-form-label">{{ entry_progress }}</label>
              <div class="col-sm-10">
                <div id="progress-backup" class="progress">
                  <div id="progress-bar" class="progress-bar"></div>
                </div>
                <div id="progress-text"></div>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <legend>{{ text_option }}</legend>
            <div class="form-group row">
              <label class="col-sm-2 col-form-label">{{ entry_export }}</label>
              <div class="col-sm-10">
                <div class="form-control" style="height: 150px; overflow: auto;">
                  {% for table in tables %}
                    <label class="form-check"><input type="checkbox" name="backup[]" value="{{ table }}" checked="checked" class="form-check-input"/> {{ table }}</label>
                  {% endfor %}
                </div>
                <button type="button" onclick="$(this).parent().find(':checkbox').prop('checked', true);" class="btn btn-link">{{ text_select_all }}</button> / <button type="button" onclick="$(this).parent().find(':checkbox').prop('checked', false);" class="btn btn-link">{{ text_unselect_all }}</button>
              </div>
            </div>
            <div class="form-group row">
              <div class="offset-sm-2 col-sm-10">
                <button type="button" id="button-backup" data-loading-text="{{ text_loading }}" class="btn btn-success"><i class="fas fa-upload"></i> {{ button_backup }}</button>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <legend>{{ text_history }}</legend>
            <div class="alert alert-info"><i class="fas fa-exclamation-circle"></i> {{ text_import }}</div>
            <div id="history"></div>
          </fieldset>
        </form>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$('#history').on('click', '.pagination a', function(e) {
	e.preventDefault();

	$('#history').load(this.href);
});

$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');
//--></script>
<script type="text/javascript"><!--
$('#button-backup').on('click', function() {
	$('#progress-bar').css('width', '0%');
	$('#progress-bar').removeClass('progress-bar-danger progress-bar-success');

	backup('index.php?route=tool/backup/backup&user_token={{ user_token }}&table=' + $('input[name^=\'backup\']:checked').first().val());
});

function backup(url) {
	$('#progress-bar').css('width', '0%');
	$('#progress-bar').removeClass('progress-bar-danger progress-bar-success');

	$.ajax({
		url: url,
		type: 'post',
		data: $('input[name^=\'backup\']:checked'),
		dataType: 'json',
		beforeSend: function() {
			$('#button-backup').button('loading');
			$('#button-upload, #history .btn').prop('disabled', true);
		},
		success: function(json) {
			$('.alert-dismissible').remove();

			if (json['progress']) {
				$('#progress-bar').css('width', json['progress'] + '%');
			}

			if (json['error']) {
				$('#progress-bar').addClass('progress-bar-danger');
				$('#progress-text').html('<div class="invalid-tooltip">' + json['error'] + '</div>');

				$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');

				$('#button-backup').button('reset');
				$('#button-upload, #history .btn').prop('disabled', false);
			}

			if (json['success']) {
				$('#progress-bar').css('width', '100%').addClass('progress-bar-success');
				$('#progress-text').html('<div class="text-success">' + json['success'] + '</div>');

				$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');

				$('#button-backup').button('reset');
				$('#button-upload, #history .btn').prop('disabled', false);
			}

			if (json['text']) {
				$('#progress-text').html(json['text']);
			}

			if (json['next']) {
				backup(json['next']);
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);

			$('#button-backup').button('reset');
			$('#button-upload, #history .btn').prop('disabled', false);
		}
	});
};

// Restore
$('#history').on('click', '.btn-warning', function() {
	$('#progress-bar').css('width', '0%');
	$('#progress-bar').removeClass('progress-bar-danger progress-bar-success');

	restore('index.php?route=tool/backup/restore&user_token={{ user_token }}&filename=' + encodeURIComponent($(this).val(), this));
});

function restore(url, element) {
	$.ajax({
		url: url,
		type: 'post',
		dataType: 'json',
		beforeSend: function() {
			$(element).button('loading');
			$('#button-upload, #button-backup, #history .btn').not(element).prop('disabled', true);
		},
		success: function(json) {
			$('.alert-dismissible').remove();

			if (json['progress']) {
				$('#progress-bar').css('width', json['progress'] + '%');
			}

			if (json['error']) {
				$('#progress-bar').addClass('progress-bar-danger');
				$('#progress-text').html('<div class="invalid-tooltip">' + json['error'] + '</div>');

				$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');

				$(element).button('reset');
				$('#button-upload, #button-backup, #history .btn').not(element).prop('disabled', false);
			}

			if (json['success']) {
				$('#progress-bar').css('width', '100%').addClass('progress-bar-success');
				$('#progress-text').html('<div class="text-success">' + json['success'] + '</div>');

				$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');

				$(element).button('reset');
				$('#button-upload, #button-backup, #history .btn').not(element).prop('disabled', false);
			}

			if (json['text']) {
				$('#progress-text').html(json['text']);
			}

			if (json['next']) {
				restore(json['next'], element);
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);

			$(element).button('reset');
			$('#button-upload, #button-backup, #history .btn').not(element).prop('disabled', false);
		}
	});
}

$('#button-upload').on('click', function() {
	$('#form-upload').remove();

	$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="upload" /></form>');

	$('#form-upload input[name=\'upload\']').trigger('click');

	if (typeof timer != 'undefined') {
		clearInterval(timer);
	}

	timer = setInterval(function() {
		if ($('#form-upload input[name=\'upload\']').val() != '') {
			clearInterval(timer);

			$.ajax({
				url: 'index.php?route=tool/backup/upload&user_token={{ user_token }}',
				type: 'post',
				dataType: 'json',
				data: new FormData($('#form-upload')[0]),
				cache: false,
				contentType: false,
				processData: false,
				beforeSend: function() {
					$('#button-upload').button('loading');
				},
				complete: function() {
					$('#button-upload').button('reset');
				},
				success: function(json) {
					$('.alert-dismissible').remove();

					if (json['error']) {
						$('#content > .container-fluid').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
					}

					if (json['success']) {
						$('#content > .container-fluid').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');

						$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
		}
	}, 500);
});

// Delete
$('#history').on('click', '.btn-danger', function() {
	var element = this;

	$.ajax({
		url: 'index.php?route=tool/backup/delete&user_token={{ user_token }}&filename=' + encodeURIComponent($(element).val()),
		type: 'post',
		dataType: 'json',
		beforeSend: function() {
			$(element).button('loading');
		},
		complete: function() {
			$(element).button('reset');
		},
		success: function(json) {
			$('.alert-dismissible').remove();

			if (json['error']) {
				$('#history').before('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
			}

			if (json['success']) {
				$('#history').before('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');

				$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});
//--></script>
{{ footer }}
